﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using GreatB2C.Utility;
using GreatB2C.Business;
using GreatB2C.Config.Shop;

namespace GreatB2C.Web.Manage.SystemSet.Contents
{
    public partial class ModelTemplate : Inherit.ManagePage
    {
        Business.ModelTemplates BLL = new ModelTemplates( );

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                if ( Request.QueryString[ "Action" ] != null && Request.QueryString[ "Action" ].ToString( ) == "Modify"
                    && Request.QueryString[ "TemplateID" ] != null && Request.QueryString[ "TemplateID" ].ToString( ) != "" )
                {
                    lblTitle.Text = "修改模型模板";
                    btnSubmit.Text = "保 存[S]";

                    LoadData( );
                }
            }
        }

        protected void LoadData( )
        {
            if ( Request.QueryString[ "TemplateID" ] != null && Request.QueryString[ "TemplateID" ].ToString( ) != "" )
            {
                GreatB2C.Model.ModelTemplates model = new GreatB2C.Model.ModelTemplates( );
                model.TemplateID = Convert.ToInt32( Request.QueryString[ "TemplateID" ].ToString( ) );

                model = BLL.ListByID( model );
                if ( model != null )
                {
                    txtTemplateName.Text = model.TemplateName;
                    txtTemplateDescription.Text = model.TemplateDescription;
                }
            }
        }

        protected void btnSubmit_Click( object sender , EventArgs e )
        {
            GreatB2C.Model.ModelTemplates model = new GreatB2C.Model.ModelTemplates( );
            model.TemplateID = -1;
            model.TemplateName = txtTemplateName.Text.Trim( );
            model.TemplateDescription = txtTemplateDescription.Text.Trim( );
            model.Field = "";
            model.IsEshop = false;

            if ( Request.QueryString[ "ModelID" ] != null && Request.QueryString[ "ModelID" ].ToString( ) != "" )
            {
                GreatB2C.Model.Model modelModel = new GreatB2C.Model.Model( );
                modelModel.ModelID = Convert.ToInt32( Request.QueryString[ "ModelID" ].ToString( ) );
                Business.Model modelBLL = new GreatB2C.Business.Model( );
                modelModel = modelBLL.ListByID( modelModel );
                if ( modelModel != null )
                {
                    model.Field = modelModel.Field;
                }
            }

            if ( Request.QueryString[ "TemplateID" ] != null && Request.QueryString[ "TemplateID" ].ToString( ) != "" )
            {
                model.TemplateID = Convert.ToInt32( Request.QueryString[ "TemplateID" ].ToString( ) );
            }

            bool isExist = BLL.IsExists( model );
            if ( isExist )
            {
                bool isSuccess = BLL.Update( model );
                if ( isSuccess )
                {
                    litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','模型模板信息修改成功！','ModelTemplateManage.aspx');</script>";
                    return;
                }
                else
                {
                    litScript.Text = "<script type='text/javascript'>ShowError('操作失败','模型模板信息修改失败！');</script>";
                    return;
                }
            }
            else
            {
                bool isSuccess = BLL.Insert( model );
                if ( isSuccess )
                {
                    litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','模型模板信息添加成功！','ModelTemplateManage.aspx');</script>";
                    return;
                }
                else
                {
                    litScript.Text = "<script type='text/javascript'>ShowError('操作失败','模型模板信息添加失败！');</script>";
                    return;
                }
            }
        }
    }
}
